const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
     properties: {
        role: {
            type: "String",
            value: ""
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        searchValue: "",
        waitPayList: [],
        isLastPage: false,
        current: 1,
        size: 10,
        qualityMap: {
            1: "精品",
            2: "次品",
            3: "统货",
        },
    },

    lifetimes: {
        attached: function () {
            // 在组件实例进入页面节点树时执行
            this.getData()
        },
        detached: function () {
            // 在组件实例被从页面节点树移除时执行
        },
    },

    methods: {
        onPullDownRefresh() {
            this.setData({
                current: 1,
                searchValue: "",
                waitPayList: [],
                isLastPage: false
            })
            this.getData()
        },
        onReachBottom() {
            let {
                current,
                isLastPage
            } = this.data
            if (isLastPage) {
                return
            }
            this.setData({
                current: (Number(current) + 1)
            })
            this.getData()
        },
        onSearch() {
            this.setData({
                current: 1,
                waitPayList: [],
                isLastPage: false
            })
            this.getData()
        },
        toDetail(e) {
            let item = e.currentTarget.dataset.item
            wx.navigateTo({
                url: `/pages/business/details/index?id=${item.id}`,
            })
        },
        syncToAccountBook(e) {
            let item = e.currentTarget.dataset.item
            item.orderType = 2
            item.type = 0
            item.financeName = "卖菜"
            this.triggerEvent('bookPicker', item)
        },
        toFarmerDetail(e) {
            let item = e.currentTarget.dataset.item
            let title = `${item.name} ${item.mobile}`
            wx.navigateTo({
              url: `/pages/payment/wait-payment/index?id=${item.userId}&title=${title}`,
            })
        },
        getData() {
            const {
                role,
                current,
                searchValue,
                size
            } = this.data;
            let api,
                params;
            if (role == 2) {
                api = "getFarmerList"
                params = {
                    current,
                    size,
                    cropName: searchValue,
                    enterStockStatus: 3
                }
            } else {
                api = "getNonSettlementPage"
                params = {
                    current,
                    size,
                    keyword: searchValue,
                    companyId: app.globalData.userInfo.companyData.companyId,
                    enterStatus: 3
                }
            }
            wx.showLoading({
                title: "正在请求数据中"
            })
            wx.$apis[api](params).then(res => {
                wx.stopPullDownRefresh();
                let oldWaitPayList = this.data.waitPayList;
                let waitPayList = res.data && res.data.records.map(item => {
                    item.createTimeStr = item.createTime && item.createTime.replace("T", " ")
                    item.settleAccountPrice = item.settleAccount && wx.$getYuan(item.settleAccount)
                    item.transactionPrice = item.transactionPrice && wx.$getYuan(item.transactionPrice)
                    item.countPrice = item.countPrice && wx.$getYuan(item.countPrice)
                    return item
                })
                if (waitPayList && waitPayList.length) {
                    let newWaitPayList = oldWaitPayList.concat(waitPayList)
                    this.setData({
                        waitPayList: newWaitPayList
                    })
                    if (waitPayList.length < size) {
                        this.setData({
                            isLastPage: true
                        })
                    }
                } else {
                    this.setData({
                        isLastPage: true
                    })
                }
            }).catch(err => {
                wx.stopPullDownRefresh();
            })
        }
    }
})